//
//  VisitorInfoController.m
//  ppkc2016
//。 商贸配对 - 展商  观众信息
//  Created by Laura on 2017/3/17.
//  Copyright © 2017年 拼凑的断音. All rights reserved.
//

#import "VisitorInfoController.h"
#import "XVCReleaseProduct.h"
#import "XXTool.h"
#import "VisitorInfoCell.h"
#import "VisitorInfoModel.h"
#import "LiuyanViewController.h"
#import "VisitorDetailControllerViewController.h"

@interface VisitorInfoController () <UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>

@property (nonatomic,strong) UIButton *testButton;
@property(nonatomic,strong) UIView * top;
@property(nonatomic ,strong) UISearchBar *search;
@property (nonatomic,strong) UITableView *tableview;

@end

@implementation VisitorInfoController{
    NSMutableArray <VisitorInfoModel*> *visiInfo;
}

-(UIView*) top{
    if(_top == nil){
        _top=[[UIView alloc]init];
        _top.backgroundColor=[UIColor colorWithRed:205/255.0 green:205/255.0 blue:205/255.0 alpha:1.0];
        [self.view addSubview:_top];
    }
    return _top;
}

-(UISearchBar*) search{
    if(_search == nil){
        _search=[[UISearchBar alloc] init];
        _search.layer.cornerRadius=5;
        _search.delegate=self;
        [_search setSearchFieldBackgroundImage:[UIImage imageNamed:@"搜索内容背景"] forState:UIControlStateNormal];
        [_search setSearchTextPositionAdjustment:UIOffsetMake(12, 0)];
        for(UIView *view in  [[[_search subviews] objectAtIndex:0] subviews])
        {
            
            if([view isKindOfClass:NSClassFromString(@"UISearchBarTextField")])
            {
                UITextField * text =(UITextField *)view;
                if([XXTool didCurrentLanguageChinese])
                {
                    text.font = [XXTool systemFontOfSize:15];
                }
                else
                {
                    text.font = [XXTool systemFontOfSize:13];
                }
            }
        }
        _search.backgroundImage = [XXTool createImageWithColor:[UIColor clearColor]];
        [_top addSubview:_search];
    }
    return _search;
}
-(UITableView*)tableview
{
    if (_tableview==nil)
    {
        _tableview = [[UITableView alloc] init];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableview.backgroundColor = [XXTool colorWithBackGround];
        MJRefreshNormalHeader *useHead = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(getData)];
        [useHead setTitle:NSLocalizedString(@"下拉刷新", nil) forState:MJRefreshStateIdle];
        [useHead setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useHead setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_header = useHead;
        
        MJRefreshBackNormalFooter *useFooter = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReFeren)];
        [useFooter setTitle:NSLocalizedString(@"刷新中...", nil) forState:MJRefreshStateRefreshing];
        [useFooter setTitle:NSLocalizedString(@"上拉刷新", nil) forState:MJRefreshStateIdle];
        [useFooter setTitle:NSLocalizedString(@"松开立即刷新", nil) forState:MJRefreshStatePulling];
        _tableview.mj_footer = useFooter;
        [self.view addSubview:_tableview];
    }
    return _tableview;
}

// 下拉 获取数据
-(void)getData{
    [VisitorInfoModel requestVisitorInfoListType:_search.text listCount:0 completion:^(BOOL isSuccess, NSMutableArray<VisitorInfoModel *> *datalist){
        if(isSuccess){
            visiInfo=datalist;
            [_tableview reloadData];
        }
        [_tableview.mj_header endRefreshing];
        [self.view stopLoading];
    }];
}

// 上拉 获取数据
-(void)footerReFeren{
    [VisitorInfoModel requestVisitorInfoListType:_search.text listCount:visiInfo.count completion:^(BOOL isSuccess, NSMutableArray<VisitorInfoModel *> *datalist){
        if(isSuccess){
            visiInfo=datalist;
            [_tableview reloadData];
        }
        [_tableview.mj_header endRefreshing];
        [self.view stopLoading];
    }];
}


-(void)initUI
{
    [self.navigationItem setTitle:NSLocalizedString(@"观众信息", nil)];
    [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"发布展品", nil) style:UIBarButtonItemStylePlain target:self action:@selector(clickFabu)]];
    [self top];
    [self search];
    visiInfo=[[NSMutableArray alloc]init];
    
    [self tableview];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickCellLiuyan:) name:@"dropLiuyan" object:nil];
    [self getData];
}

-(void)clickCellLiuyan:(NSNotification *)notification{
    
    NSString *tagg=[NSString stringWithFormat:@"%d",[[notification.userInfo objectForKey:@"tag"] integerValue]];
    [LiuyanViewController pushVisitorInfoWithSourceId:self liuyan_id:tagg Type:XVCExhi];
    
}

/**
 *  点击发布展品
 */
-(void)clickFabu{
    [self.navigationController pushViewController:[[XVCReleaseProduct alloc] init] animated:YES];
}

-(void)refreshLayout:(CGRect)frame
{
    _top.frame=CGRectMake(0, 0, frame.size.width, 50);
    _search.frame=CGRectMake(5, 5, frame.size.width-10, 50-10);
    _tableview.frame=CGRectMake(0, 55, frame.size.width, frame.size.height-65);
}

#pragma mark tableviewDatasource
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* reuseIdentifier = @"ProductInformationShowCell";
    VisitorInfoCell *cell=[tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if(cell == nil){
        cell=[[VisitorInfoCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        [cell setCellWithData:visiInfo[indexPath.row]];
        
    }
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 102;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return visiInfo.count;
}

#pragma mark tableviewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    VisitorInfoModel *temp = visiInfo[indexPath.row];
    [VisitorDetailControllerViewController pushVisitorDetailVc:self visitorId:temp.orderid];
}

-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    //    if ([_delegate respondsToSelector:@selector(conditionalSearch:serachBarDidClickSearchButtonWithSearchText:)])
    //    {
    //        [_delegate conditionalSearch:self serachBarDidClickSearchButtonWithSearchText:_searchbar.text];
    //    }
    NSLog(@"点击搜索");
    [self getData];
    [searchBar resignFirstResponder]; //关闭键盘
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}

@end
